//
//  ParsePendingRelationshipsXMLReader.m
//  cliqset
//
//  Created by David Hinson on 4/2/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "ParsePendingRelationshipsXMLReader.h"

@implementation ParsePendingRelationshipsXMLReader

@synthesize mode                        = _mode;
@synthesize contentOfCurrentProperty    = _contentOfCurrentProperty;
@synthesize currentTargetObject         = _currentTargetObject;

NSString *strRelationshipRequest        = @"RelationshipRequest";
NSString *strPendingUserIdentifier      = @"UserIdentifier";
NSString *strPendingGivenName           = @"GivenName";
NSString *strPendingFamilyName          = @"FamilyName";
NSString *strPendingGender              = @"Gender";
NSString *strPendingAvatar              = @"Avatar";
NSString *strPendingHometown            = @"Hometown";

-(id)init {
	[super init];
	
	return self;
}

-(void)resetVars {
	
	self.contentOfCurrentProperty = nil;
	
	if (@selector(_currentTargetObject) && _currentTargetObject!=nil && self.currentTargetObject) {
		[self.currentTargetObject release];
		self.currentTargetObject = nil;
	}
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	[self resetVars];
}

- (void)parseXMLFileAtData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
	
	[self resetVars];
    
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }
	
	if ([elementName isEqualToString:strRelationshipRequest]) {		
		
		[self resetVars];		
		self.currentTargetObject = [[Target alloc] init];
		
	} else if ([elementName isEqualToString:strPendingUserIdentifier]) {
        self.contentOfCurrentProperty = [NSMutableString string];
 	} else if ([elementName isEqualToString:strPendingGivenName]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strPendingFamilyName]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strPendingGender]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strPendingAvatar]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strPendingHometown]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else {
        // The element isn't one that we care about, so set the property that holds the 
        // character content of the current element to nil. That way, in the parser:foundCharacters:
        // callback, the string that the parser reports will be ignored.
        self.contentOfCurrentProperty = nil;
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     	
    if (qName) {
        elementName = qName;
    }
	
	// Subject Identifier
    if ([elementName isEqualToString:strPendingUserIdentifier]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.userIdentifier = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.userIdentifier = (NSMutableString*)@"";
		}
	}
	
	// Given Name
    if ([elementName isEqualToString:strPendingGivenName]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.givenName = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.givenName = (NSMutableString*)@"";
		}
	}	
	
	// Family Name
    if ([elementName isEqualToString:strPendingFamilyName]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.familyName = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.familyName = (NSMutableString*)@"";
		}
	}	
	
	// Gender
    if ([elementName isEqualToString:strPendingGender]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.gender = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.gender = (NSMutableString*)@"";
		}
	}	
	
	// Avatar
    if ([elementName isEqualToString:strPendingAvatar]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.avatarURL  = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.avatarURL  = (NSMutableString*)@"";
			self.currentTargetObject.avatarBlob = nil;
		}
	}	
	
	// Hometown Locality
    if ([elementName isEqualToString:strPendingHometown]) {
		if (self.contentOfCurrentProperty) {
			self.currentTargetObject.hometown = self.contentOfCurrentProperty;
		} else {
			self.currentTargetObject.hometown = (NSMutableString*)@"";
		}
	}	
	
	if ([elementName isEqualToString:strRelationshipRequest]) {
		// we will write the Relationship to an array here...
		if (self.currentTargetObject) {
			if (self.mode == 31) {
				[(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addPendingRelationship:) withObject:self.currentTargetObject waitUntilDone:YES];
				}
			if (self.mode == 37) {
				[(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addAskedForRelationship:) withObject:self.currentTargetObject waitUntilDone:YES];
				}
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentProperty) {
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
		if (string) {
			string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
			string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
			string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
			[self.contentOfCurrentProperty appendString:string];
		}
    }
}

-(void) dealloc {
	[_contentOfCurrentProperty release];
	[_currentTargetObject release];	
	[super dealloc];
}

@end
